﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Event_SG_EWDT
{
    public partial class UpdateGroup : System.Web.UI.Page
    {
        string groupid = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            //TODO IF NOT OWNER OF TIS GROUP REDIRECT!!!!

            groupid = Request.QueryString["groupid"];
            Session["Group"] = groupid;
            if (Session["Member"] == null || groupid == "")
            {
                Session["flag"] = "redirecterror";
                Response.Redirect("~/registration.aspx");
            }

            if (!GroupControl.IsAdmin(Session["Member"].ToString(), groupid))
            {
                Session["flag"] = "notauthorizedgroup";
                Response.Redirect("~/registration.aspx");
            }

            if (!IsPostBack)
            {
                if (groupid != "")
                {
                    Group gr = GroupControl.RetrieveGroup(groupid);

                    if (gr.GroupId == null)
                    {
                        Session["flag"] = "notfound";
                        Response.Redirect("~/index.aspx");
                    }

                    tbxUpdateGroupName.Text = gr.GroupName;
                    tbxUpdateGroupDesc.Text = gr.Description;
                    ddlUpdateGroupCat.SelectedValue = gr.GroupTypeId;

                    if (gr.GroupPrivacy == 0)
                    {
                        rdblUpdateGroupPrivacy.SelectedIndex = 0;
                    }
                    else if (gr.GroupPrivacy == 1)
                    {
                        rdblUpdateGroupPrivacy.SelectedIndex = 1;
                    }
                    else if (gr.GroupPrivacy == 2)
                    {
                        rdblUpdateGroupPrivacy.SelectedIndex = 2;
                    }
                }
            }
            if (!IsPostBack)
            {
                TabContainer1.ActiveTabIndex = 0;
            }
        }

        protected void btnUpdateGroup_Click(object sender, EventArgs e)
        {
            bool up = GroupControl.UpdateGroup(groupid, tbxUpdateGroupName.Text, tbxUpdateGroupDesc.Text, rdblUpdateGroupPrivacy.SelectedIndex, ddlUpdateGroupCat.SelectedValue);

            if (fuUpdateGroupLogo.HasFile)
                {
                    Utility.UploadGroupLogo(fuUpdateGroupLogo, groupid);
                }
              
                if (fuUpdateGroupThumbnail.HasFile)
                {
                    Utility.UploadGroupThumbnail(fuUpdateGroupThumbnail, groupid);
                }
               
            if (up)
            {

                //Notifications
                Utility.SendUpdateNotificationToGroupMembers(Request, Convert.ToInt32(groupid));


                Session["flag"] = "updated";
                Response.Redirect("~/ViewGroup.aspx?groupid=" + groupid);
            }
            else
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "toast", "$().toastmessage('showErrorToast', 'Opps! One of more required fields were invalid!');", true);
            }
        }

        protected void gvWhoIsInGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            string id = gvWhoIsInGroup.SelectedRow.Cells[0].Text;
            GroupControl.UpdateMemberGroupStatus(id, groupid, 2);
            gvWhoIsInGroup.DataBind();
        }

        protected void gvPendingGroupList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int index = Convert.ToInt32(e.CommandArgument);
            GridViewRow row = gvPendingGroupList.Rows[index];
            if (e.CommandName == "Accept")
            {
                
                //EventControl.EventApproval(row.Cells[0].Text, row.Cells[2].Text, 0);
                GroupControl.UpdateMemberGroupStatus(row.Cells[0].Text, row.Cells[2].Text, 1);

                Utility.SendAcceptedToGroup(Convert.ToInt32(Session["Member"]), Convert.ToInt32(row.Cells[0].Text), row.Cells[2].Text);
            }
            else if (e.CommandName == "Reject")
            {
                MemberGroup mg = new MemberGroup();
                mg.MembershipNo = row.Cells[0].Text;
                mg.GroupId= row.Cells[2].Text;

                DBMANAGER.DeleteMemberGroup(mg);
            }
            gvPendingGroupList.DataBind();
            gvWhoIsInGroup.DataBind();
        }


    }

}